﻿
namespace AR.Web.Services
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.ComponentModel.DataAnnotations;
    using System.Data;
    using System.Linq;
    using System.ServiceModel.DomainServices.EntityFramework;
    using System.ServiceModel.DomainServices.Hosting;
    using System.ServiceModel.DomainServices.Server;
    using AR.Entities;


    // Implements application logic using the AR_DBEntities3 context.
    // TODO: Add your application logic to these methods or in additional methods.
    // TODO: Wire up authentication (Windows/ASP.NET Forms) and uncomment the following to disable anonymous access
    // Also consider adding roles to restrict access as appropriate.
    // [RequiresAuthentication]
    [EnableClientAccess()]
    public class EntretienTechniqueService : LinqToEntitiesDomainService<AR_DBEntities>
    {
        public CANDIDAT GetCandidatByCodeTest(string codetest)
        {
            return this.ObjectContext.CANDIDAT.Include("PRACTICE.TEST_TECHNIQUE").Where(_ => _.CODE_TEST == codetest).SingleOrDefault();
        }
        // TODO:
        // Consider constraining the results of your query method.  If you need additional input you can
        // add parameters to this method or create additional query methods with different names.
        // To support paging you will need to add ordering to the 'CANDIDAT' query.
        public IQueryable<CANDIDAT> GetCANDIDAT()
        {
            return this.ObjectContext.CANDIDAT;
        }

        public void InsertCANDIDAT(CANDIDAT cANDIDAT)
        {
            if ((cANDIDAT.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(cANDIDAT, EntityState.Added);
            }
            else
            {
                this.ObjectContext.CANDIDAT.AddObject(cANDIDAT);
            }
        }

        public void UpdateCANDIDAT(CANDIDAT currentCANDIDAT)
        {
            this.ObjectContext.CANDIDAT.AttachAsModified(currentCANDIDAT, this.ChangeSet.GetOriginal(currentCANDIDAT));
        }

        public void DeleteCANDIDAT(CANDIDAT cANDIDAT)
        {
            if ((cANDIDAT.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(cANDIDAT, EntityState.Deleted);
            }
            else
            {
                this.ObjectContext.CANDIDAT.Attach(cANDIDAT);
                this.ObjectContext.CANDIDAT.DeleteObject(cANDIDAT);
            }
        }

        // TODO:
        // Consider constraining the results of your query method.  If you need additional input you can
        // add parameters to this method or create additional query methods with different names.
        // To support paging you will need to add ordering to the 'CHOIX' query.
        public IQueryable<CHOIX> GetCHOIX()
        {
            return this.ObjectContext.CHOIX;
        }

        public void InsertCHOIX(CHOIX cHOIX)
        {
            if ((cHOIX.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(cHOIX, EntityState.Added);
            }
            else
            {
                this.ObjectContext.CHOIX.AddObject(cHOIX);
            }
        }

        public void UpdateCHOIX(CHOIX currentCHOIX)
        {
            this.ObjectContext.CHOIX.AttachAsModified(currentCHOIX, this.ChangeSet.GetOriginal(currentCHOIX));
        }

        public void DeleteCHOIX(CHOIX cHOIX)
        {
            if ((cHOIX.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(cHOIX, EntityState.Deleted);
            }
            else
            {
                this.ObjectContext.CHOIX.Attach(cHOIX);
                this.ObjectContext.CHOIX.DeleteObject(cHOIX);
            }
        }

        // TODO:
        // Consider constraining the results of your query method.  If you need additional input you can
        // add parameters to this method or create additional query methods with different names.
        // To support paging you will need to add ordering to the 'PRACTICE' query.
        public IQueryable<PRACTICE> GetPRACTICE()
        {
            return this.ObjectContext.PRACTICE;
        }

        public IQueryable<PRACTICE> GetPRACTICEs()
        {
            return this.ObjectContext.PRACTICE.Include("TEST_TECHNIQUE");
        }
        public void InsertPRACTICE(PRACTICE pRACTICE)
        {
            if ((pRACTICE.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(pRACTICE, EntityState.Added);
            }
            else
            {
                this.ObjectContext.PRACTICE.AddObject(pRACTICE);
            }
        }

        public void UpdatePRACTICE(PRACTICE currentPRACTICE)
        {
            this.ObjectContext.PRACTICE.AttachAsModified(currentPRACTICE, this.ChangeSet.GetOriginal(currentPRACTICE));
        }

        public void DeletePRACTICE(PRACTICE pRACTICE)
        {
            if ((pRACTICE.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(pRACTICE, EntityState.Deleted);
            }
            else
            {
                this.ObjectContext.PRACTICE.Attach(pRACTICE);
                this.ObjectContext.PRACTICE.DeleteObject(pRACTICE);
            }
        }

        // TODO:
        // Consider constraining the results of your query method.  If you need additional input you can
        // add parameters to this method or create additional query methods with different names.
        // To support paging you will need to add ordering to the 'QUESTIONS' query.
        public IQueryable<QUESTIONS> GetQUESTIONS()
        {
            return this.ObjectContext.QUESTIONS;
        }

        public void InsertQUESTIONS(QUESTIONS qUESTIONS)
        {
            if ((qUESTIONS.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(qUESTIONS, EntityState.Added);
            }
            else
            {
                this.ObjectContext.QUESTIONS.AddObject(qUESTIONS);
            }
        }

        public void UpdateQUESTIONS(QUESTIONS currentQUESTIONS)
        {
            this.ObjectContext.QUESTIONS.AttachAsModified(currentQUESTIONS, this.ChangeSet.GetOriginal(currentQUESTIONS));
        }

        public void DeleteQUESTIONS(QUESTIONS qUESTIONS)
        {
            if ((qUESTIONS.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(qUESTIONS, EntityState.Deleted);
            }
            else
            {
                this.ObjectContext.QUESTIONS.Attach(qUESTIONS);
                this.ObjectContext.QUESTIONS.DeleteObject(qUESTIONS);
            }
        }

        // TODO:
        // Consider constraining the results of your query method.  If you need additional input you can
        // add parameters to this method or create additional query methods with different names.
        // To support paging you will need to add ordering to the 'REPONSE_TEST_TECHNIQUE_CANDIDAT' query.
        public IQueryable<REPONSE_TEST_TECHNIQUE_CANDIDAT> GetREPONSE_TEST_TECHNIQUE_CANDIDAT()
        {
            return this.ObjectContext.REPONSE_TEST_TECHNIQUE_CANDIDAT.Include("TEST_TECHNIQUE").Include("CHOIX");
        }

        public IQueryable<REPONSE_TEST_TECHNIQUE_CANDIDAT> GetREPONSE_TEST_TECHNIQUE_CANDIDATByid(int id)
        {
            return this.ObjectContext.REPONSE_TEST_TECHNIQUE_CANDIDAT.Where(_ => _.ID_CANDIDAT == id);
                //.Include("TEST_TECHNIQUE.THEMES__TEST_TECHNIQUE.THEME.QUESTIONS.CHOIX").Where(_ => _.ID_CANDIDAT == id);
        }

        public void InsertREPONSE_TEST_TECHNIQUE_CANDIDAT(REPONSE_TEST_TECHNIQUE_CANDIDAT rEPONSE_TEST_TECHNIQUE_CANDIDAT)
        {
            if ((rEPONSE_TEST_TECHNIQUE_CANDIDAT.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(rEPONSE_TEST_TECHNIQUE_CANDIDAT, EntityState.Added);
            }
            else
            {
                this.ObjectContext.REPONSE_TEST_TECHNIQUE_CANDIDAT.AddObject(rEPONSE_TEST_TECHNIQUE_CANDIDAT);
            }
        }

        public void UpdateREPONSE_TEST_TECHNIQUE_CANDIDAT(REPONSE_TEST_TECHNIQUE_CANDIDAT currentREPONSE_TEST_TECHNIQUE_CANDIDAT)
        {
            this.ObjectContext.REPONSE_TEST_TECHNIQUE_CANDIDAT.AttachAsModified(currentREPONSE_TEST_TECHNIQUE_CANDIDAT, this.ChangeSet.GetOriginal(currentREPONSE_TEST_TECHNIQUE_CANDIDAT));
        }

        public void DeleteREPONSE_TEST_TECHNIQUE_CANDIDAT(REPONSE_TEST_TECHNIQUE_CANDIDAT rEPONSE_TEST_TECHNIQUE_CANDIDAT)
        {
            if ((rEPONSE_TEST_TECHNIQUE_CANDIDAT.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(rEPONSE_TEST_TECHNIQUE_CANDIDAT, EntityState.Deleted);
            }
            else
            {
                this.ObjectContext.REPONSE_TEST_TECHNIQUE_CANDIDAT.Attach(rEPONSE_TEST_TECHNIQUE_CANDIDAT);
                this.ObjectContext.REPONSE_TEST_TECHNIQUE_CANDIDAT.DeleteObject(rEPONSE_TEST_TECHNIQUE_CANDIDAT);
            }
        }

        // TODO:
        // Consider constraining the results of your query method.  If you need additional input you can
        // add parameters to this method or create additional query methods with different names.
        // To support paging you will need to add ordering to the 'TEST_TECHNIQUE' query.
        public IQueryable<TEST_TECHNIQUE> GetTEST_TECHNIQUE()
        {
            return this.ObjectContext.TEST_TECHNIQUE;
        }
        public IQueryable<TEST_TECHNIQUE> GetTEST_TECHNIQUEs()
        {
            return this.ObjectContext.TEST_TECHNIQUE.Include("THEMES__TEST_TECHNIQUE.THEME");
        }
        
        public void InsertTEST_TECHNIQUE(TEST_TECHNIQUE tEST_TECHNIQUE)
        {
            if ((tEST_TECHNIQUE.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(tEST_TECHNIQUE, EntityState.Added);
            }
            else
            {
                this.ObjectContext.TEST_TECHNIQUE.AddObject(tEST_TECHNIQUE);
            }
        }

        public void UpdateTEST_TECHNIQUE(TEST_TECHNIQUE currentTEST_TECHNIQUE)
        {
            this.ObjectContext.TEST_TECHNIQUE.AttachAsModified(currentTEST_TECHNIQUE, this.ChangeSet.GetOriginal(currentTEST_TECHNIQUE));
        }

        public void DeleteTEST_TECHNIQUE(TEST_TECHNIQUE tEST_TECHNIQUE)
        {
            if ((tEST_TECHNIQUE.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(tEST_TECHNIQUE, EntityState.Deleted);
            }
            else
            {
                this.ObjectContext.TEST_TECHNIQUE.Attach(tEST_TECHNIQUE);
                this.ObjectContext.TEST_TECHNIQUE.DeleteObject(tEST_TECHNIQUE);
            }
        }

        // TODO:
        // Consider constraining the results of your query method.  If you need additional input you can
        // add parameters to this method or create additional query methods with different names.
        // To support paging you will need to add ordering to the 'TEST_TECHNIQUE_CANDIDAT' query.
       
        
        public IQueryable<TEST_TECHNIQUE_CANDIDAT> GetTEST_TECHNIQUE_CANDIDAT()
        {
          
            return this.ObjectContext.TEST_TECHNIQUE_CANDIDAT;
        }
        public TEST_TECHNIQUE GetTEST_TECHNIQUE_CANDIDATByid(int id)
        {
            return this.ObjectContext.CANDIDAT.Include("TEST_TECHNIQUE").Where(_ => _.ID_CANDIDAT == id).SingleOrDefault().TEST_TECHNIQUE;            
        }
        
        public TEST_TECHNIQUE GetTEST_TECHNIQUE_CANDIDATByTestCode(string code_test)
        {
            return this.ObjectContext.CANDIDAT.Include("TEST_TECHNIQUE").Where(_ => _.CODE_TEST == code_test).SingleOrDefault().TEST_TECHNIQUE;
        }
     
        //public IQueryable<THEMES__TEST_TECHNIQUE> GetTHEMES__TEST_TECHNIQUE
        //{

        public IQueryable<THEMES__TEST_TECHNIQUE> GetTHEMES__TEST_TECHNIQUE()
        {

            return this.ObjectContext.THEMES__TEST_TECHNIQUE;
        }
        public void InsertTHEME_TEST_TECHNIQUE(THEMES__TEST_TECHNIQUE tHEMES__TEST_TECHNIQUE)
        {
            if ((tHEMES__TEST_TECHNIQUE.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(tHEMES__TEST_TECHNIQUE, EntityState.Added);
            }
            else
            {
                this.ObjectContext.THEMES__TEST_TECHNIQUE.AddObject(tHEMES__TEST_TECHNIQUE);
            }
        }

        public void UpdateTHEME_TEST_TECHNIQUE(THEMES__TEST_TECHNIQUE currentTHEME_TEST_TECHNIQUE)
        {
            this.ObjectContext.THEMES__TEST_TECHNIQUE.AttachAsModified(currentTHEME_TEST_TECHNIQUE, this.ChangeSet.GetOriginal(currentTHEME_TEST_TECHNIQUE));
        }

        public void DeleteTHEME_TEST_TECHNIQUE(THEMES__TEST_TECHNIQUE tHEME_TEST_TECHNIQUE)
        {
            if ((tHEME_TEST_TECHNIQUE.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(tHEME_TEST_TECHNIQUE, EntityState.Deleted);
            }
            else
            {
                this.ObjectContext.THEMES__TEST_TECHNIQUE.Attach(tHEME_TEST_TECHNIQUE);
                this.ObjectContext.THEMES__TEST_TECHNIQUE.DeleteObject(tHEME_TEST_TECHNIQUE);
            }
        }
        public THEME GetTHEMEByID(int id)
        {
            return this.ObjectContext.THEME.Where(_ => _.ID_THEME == id).SingleOrDefault();
        }


        public IQueryable<THEME> GetTEST_TECHNIQUE_CANDIDAT2(int id)
        {
            IQueryable<THEME>  query = from THEME theme in
                       this.ObjectContext.THEME
                   join testtheme in this.ObjectContext.THEMES__TEST_TECHNIQUE on theme.ID_THEME equals testtheme.ID_THEME
                   join cand in this.ObjectContext.CANDIDAT on testtheme.ID_TEST equals cand.ID_TEST
                   where cand.ID_CANDIDAT == id
                   select theme;
            return this.ObjectContext.THEME.Include("THEMES__TEST_TECHNIQUE").Include("QUESTIONS.CHOIX").Where(x => query.Select(y => y.ID_THEME).Contains(x.ID_THEME));
        }

        public void InsertTEST_TECHNIQUE_CANDIDAT(TEST_TECHNIQUE_CANDIDAT tEST_TECHNIQUE_CANDIDAT)
        {
            if ((tEST_TECHNIQUE_CANDIDAT.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(tEST_TECHNIQUE_CANDIDAT, EntityState.Added);
            }
            else
            {
                this.ObjectContext.TEST_TECHNIQUE_CANDIDAT.AddObject(tEST_TECHNIQUE_CANDIDAT);
            }
        }

        public void UpdateTEST_TECHNIQUE_CANDIDAT(TEST_TECHNIQUE_CANDIDAT currentTEST_TECHNIQUE_CANDIDAT)
        {
            this.ObjectContext.TEST_TECHNIQUE_CANDIDAT.AttachAsModified(currentTEST_TECHNIQUE_CANDIDAT, this.ChangeSet.GetOriginal(currentTEST_TECHNIQUE_CANDIDAT));
        }

        public void DeleteTEST_TECHNIQUE_CANDIDAT(TEST_TECHNIQUE_CANDIDAT tEST_TECHNIQUE_CANDIDAT)
        {
            if ((tEST_TECHNIQUE_CANDIDAT.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(tEST_TECHNIQUE_CANDIDAT, EntityState.Deleted);
            }
            else
            {
                this.ObjectContext.TEST_TECHNIQUE_CANDIDAT.Attach(tEST_TECHNIQUE_CANDIDAT);
                this.ObjectContext.TEST_TECHNIQUE_CANDIDAT.DeleteObject(tEST_TECHNIQUE_CANDIDAT);
            }
        }

        // TODO:
        // Consider constraining the results of your query method.  If you need additional input you can
        // add parameters to this method or create additional query methods with different names.
        // To support paging you will need to add ordering to the 'THEME' query.
      
        public ICollection<THEME> GetTHEME()
        {
            var query = from THEME item in
                            this.ObjectContext.THEME.Include("QUESTIONS.CHOIX")
                       select item;
            return query.ToList();
        }
        public void InsertTHEME(THEME tHEME)
        {
            if ((tHEME.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(tHEME, EntityState.Added);
            }
            else
            {
                this.ObjectContext.THEME.AddObject(tHEME);
            }
        }

        public void UpdateTHEME(THEME currentTHEME)
        {
            this.ObjectContext.THEME.AttachAsModified(currentTHEME, this.ChangeSet.GetOriginal(currentTHEME));
        }

        public void DeleteTHEME(THEME tHEME)
        {
            if ((tHEME.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(tHEME, EntityState.Deleted);
            }
            else
            {
                this.ObjectContext.THEME.Attach(tHEME);
                this.ObjectContext.THEME.DeleteObject(tHEME);
            }
        }

        // TODO:
        // Consider constraining the results of your query method.  If you need additional input you can
        // add parameters to this method or create additional query methods with different names.
        // To support paging you will need to add ordering to the 'Users' query.
        public IQueryable<Users> GetUsers()
        {
            return this.ObjectContext.Users;
        }

        public void InsertUsers(Users users)
        {
            if ((users.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(users, EntityState.Added);
            }
            else
            {
                this.ObjectContext.Users.AddObject(users);
            }
        }

        public void UpdateUsers(Users currentUsers)
        {
            this.ObjectContext.Users.AttachAsModified(currentUsers, this.ChangeSet.GetOriginal(currentUsers));
        }

        public void DeleteUsers(Users users)
        {
            if ((users.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(users, EntityState.Deleted);
            }
            else
            {
                this.ObjectContext.Users.Attach(users);
                this.ObjectContext.Users.DeleteObject(users);
            }
        }
        public TEST_TECHNIQUE_CANDIDAT GetTestCandidatByID(int idtest,int idcandidat)
        {
            return this.ObjectContext.TEST_TECHNIQUE_CANDIDAT.Where(_ => _.ID_TEST_TECHNIQUE == idtest && _.ID_CANDIDAT==idcandidat).SingleOrDefault();
        }
    }
}


